package KyleBot;

import battlecode.common.Clock;
import battlecode.common.Direction;
import battlecode.common.GameActionException;
import battlecode.common.GameConstants;
import battlecode.common.MapLocation;
import battlecode.common.RobotController;
import battlecode.common.RobotType;
import battlecode.common.Team;

public class BOTSoldier implements BOT
{
	int minesHit = 0;
	Direction current = Direction.NONE;
	
	public void RunBot(RobotController rc) throws GameActionException
	{
		while (rc.getType() == RobotType.SOLDIER) {
			try {
				if (rc.isActive()) {
					PlowTowardsLocation(rc, rc.senseEnemyHQLocation());
					Direction dir = AimAtEnemyHQ(rc);
					if (Clock.getRoundNum() < 100) {
						dir = AimAtNearestCapturePoint(rc);//AimAtRV(rc);
					}
					
					/*if (rc.senseMine(rc.getLocation().add(dir))!=rc.getTeam()) {
						rc.setIndicatorString(1, "difuse " + dir.name());
						rc.defuseMine(rc.getLocation().add(dir));
					}*/
					
					rc.move(dir);
				} else {
					rc.yield();
				}
				

				// End turn
				//rc.yield();
			} catch (Exception e) {
				e.printStackTrace();
				rc.yield();
			}
		}
	}
	
	public Direction NextDir(Direction dir, boolean isClockwise) {
		if (isClockwise) {
			if (dir == Direction.NORTH) {
				return Direction.NORTH_EAST;
			} else if (dir == Direction.NORTH_WEST) {
				return Direction.NORTH;
			} else if (dir == Direction.WEST) {
				return Direction.NORTH_WEST;
			} else if (dir == Direction.SOUTH_WEST) {
				return Direction.WEST;
			} else if (dir == Direction.SOUTH) {
				return Direction.SOUTH_WEST;
			} else if (dir == Direction.SOUTH_EAST) {
				return Direction.SOUTH;
			} else if (dir == Direction.EAST) {
				return Direction.SOUTH_EAST;
			} else if (dir == Direction.NORTH_EAST) {
				return Direction.EAST;
			}
		} else {
			if (dir == Direction.EAST) {
				return Direction.NORTH_EAST;
			} else if (dir == Direction.NORTH_EAST) {
				return Direction.NORTH;
			} else if (dir == Direction.NORTH) {
				return Direction.NORTH_WEST;
			} else if (dir == Direction.NORTH_WEST) {
				return Direction.WEST;
			} else if (dir == Direction.WEST) {
				return Direction.SOUTH_WEST;
			} else if (dir == Direction.SOUTH_WEST) {
				return Direction.SOUTH;
			} else if (dir == Direction.SOUTH) {
				return Direction.SOUTH_EAST;
			} else if (dir == Direction.SOUTH_EAST) {
				return Direction.EAST;
			}
		}
		
		return dir;
	}
	
	public Direction AimAtEnemyHQ(RobotController rc) {
		Direction dir = rc.getLocation().directionTo(rc.senseEnemyHQLocation());
		
		while (!(rc.senseMine(rc.getLocation().add(dir))==null && rc.canMove(dir))) {
			dir = NextDir(dir, true);
		}
		
		return dir;
	}
	
	public Direction AimAtRV(RobotController rc) {
		//MapLocation rv = rc.senseHQLocation().add(rc.senseHQLocation().directionTo(rc.senseEnemyHQLocation())).add(rc.senseHQLocation().directionTo(rc.senseEnemyHQLocation())).add(rc.senseHQLocation().directionTo(rc.senseEnemyHQLocation()));
		Direction dir = rc.getLocation().directionTo((new MapLocation(12,10)));
		
		while (!(dir != Direction.NONE && rc.senseMine(rc.getLocation().add(dir))==null && rc.canMove(dir))) {
			dir = NextDir(dir, true);
		}
		
		return dir;
	}
	
	public Direction AimAtNearestCapturePoint(RobotController rc) {

		MapLocation[] encampments = rc.senseAllEncampmentSquares();
		MapLocation nearest = rc.senseEnemyHQLocation(); 
		int minDist = GameConstants.MAP_MAX_HEIGHT + GameConstants.MAP_MAX_WIDTH;
		int thisDist = 0;

		for (MapLocation ml : encampments) {
			if (minDist > (thisDist = ml.distanceSquaredTo(rc.getLocation())) ) {//&& rc.senseObjectAtLocation(ml).getTeam() != rc.getTeam()) {
				minDist = thisDist;
				nearest = ml;
			}
		}

		Direction dir = rc.getLocation().directionTo(nearest);

		if (rc.senseEncampmentSquare(rc.getLocation().add(dir))) {
			try {
				rc.captureEncampment(RobotType.SUPPLIER);
			} catch (GameActionException e) {
				e.printStackTrace();
			}
		}


		//boolean detectMine = false;

		while (!(rc.senseMine(rc.getLocation().add(dir))==null && rc.canMove(dir))) {
			//if (minesHit > 9) {
				//dir = rc.getLocation().directionTo(rc.senseEnemyHQLocation());
				//break;
			//} else {
				dir = NextDir(dir, true);
			//}
			//detectMine = true;
		}

		//if (!detectMine) {
		//	minesHit = 0;
		//} else {
		//	minesHit++;
		//}

		return dir;
	}
	
	
	public void PlowTowardsLocation(RobotController rc, MapLocation destination) {
		int movesSinceDefuse = 0;
		
		while (true) {
			try {
				
				if (movesSinceDefuse > 2 && rc.getLocation().distanceSquaredTo(destination) > 3) {
					while (!rc.isActive()) {
						rc.yield();
					}
					rc.layMine();
				}
				Direction dir = rc.getLocation().directionTo(destination);
				MapLocation next = rc.getLocation().add(dir);
				
				Team t = rc.senseMine(next);
				if (t != null && t != rc.getTeam()) {
					while (!rc.isActive()) {
						rc.yield();
					}
					movesSinceDefuse = 0; 
					rc.defuseMine(next);
				}
				
				while (!rc.canMove(dir)) {
					dir = NextDir(dir, true);
				}

				while (!rc.isActive()) {
					rc.yield();
				}
				movesSinceDefuse++;
				while (!rc.canMove(dir)) {
					dir = NextDir(dir,true);
				}
				rc.move(dir);
				rc.yield();
			} catch (Exception e) {
				e.printStackTrace();
				rc.breakpoint();
				rc.yield();
			}
		}
	}
}
